from widgetOrganizer import WidgetOrganizer
from settingChanger import SettingChanger
import gtk

class MainMenu(WidgetOrganizer, gtk.MenuBar):

    def __init__(self, ancestor):
        gtk.MenuBar.__init__(self)
        WidgetOrganizer.__init__(self, 'MainMenu', ancestor)
        
        menus = [Menu_File(self), Menu_Edit(self), Menu_Help(self)]
        
        for item in menus:
            self.addChild(item)
            self.add(item)


class Submenu(WidgetOrganizer, gtk.MenuItem):
    
    def __init__(self, ancestor, caption, entries):
        gtk.MenuItem.__init__(self, caption)
        WidgetOrganizer.__init__(self, 'Menu_File', ancestor)
        
        menu = gtk.Menu()
        self.set_submenu(menu)
        
        for name, caption in entries:
            if name=='<seperator />':
                item = gtk.SeparatorMenuItem()
            else:
                item = gtk.MenuItem(caption)
                item.connect('activate',eval('self._event_'+name))
            item.show()
            menu.append(item)


class Menu_File(Submenu):
    
    def __init__(self, ancestor):
        entries = [
                  ('new','New'),
                  ('open','Open'),
                  ('<seperator />', ''),
                  ('save','Save'),
                  ('saveAs','Save as'),
                  ('saveAll','Save all'),
                  ('export','Export')
                  ]
        Submenu.__init__(self, ancestor, 'File', entries)       
    
    def _event_new(self, widget):
        self.getAncestor('MainVBox').notebook.addWorksheet()
    
    def _event_open(self, widget):
        self.getAncestor('MainVBox').notebook.openWorksheet()
    
    def _event_save(self, widget):
        self.getAncestor('MainVBox').notebook.saveWorksheet()
    
    def _event_saveAs(self, widget):
        self.getAncestor('MainVBox').notebook.saveWorksheetAs()
    
    def _event_saveAll(self, widget):
        self.getAncestor('MainVBox').notebook.saveWorksheet('all')
    
    def _event_export(self, widget):
        print 'not yet implemented'
  
      
class Menu_Edit(Submenu):
    
    def __init__(self, ancestor):
        entries = [
                   ('settings','Settings')
                   ]
        Submenu.__init__(self, ancestor, 'Edit', entries)
        
    def _event_settings(self, widget):
        SettingChanger(self.settingOrganizer)
        
    
class Menu_Help(Submenu):
    
    def __init__(self, ancestor):
        entries = [
                   ('about','About')
                   ]
        Submenu.__init__(self, ancestor, 'Help', entries)
        
    def _event_about(self, widget):
        pass